package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.SharedpreferenceUtil;
import com.yuwj.chinaa.util.ToastUtil;
import com.yuwj.chinaa.util.ValidatonCheckUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 账户邮箱绑定界面
 */
public class AccountEmailBindActivity extends BaseActivity {

    @BindView(R.id.et_email_address)
    EditText etEmailAddress;
    @BindView(R.id.et_authentication_code)
    EditText etAuthenticationCode;
    @BindView(R.id.tv_send_verification_code)
    TextView tvSendVerificationCode;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;

    IPresenter presenter;
    private CountDownTimer timer = new CountDownTimer(1000 * 60, 1000) {
        //参数1，设置倒计时的总时间（毫秒）
        //参数2，设置每次减去多少毫秒
        @Override
        public void onTick(long millisUntilFinished) {
            tvSendVerificationCode.setEnabled(false);
            tvSendVerificationCode.setText((millisUntilFinished / 1000) + "s");
        }

        @Override
        public void onFinish() {
            tvSendVerificationCode.setEnabled(true);
            tvSendVerificationCode.setText("发送验证码");
        }
    };


    public static Intent createIntent(Context context) {
        return new Intent(context, AccountEmailBindActivity.class);
    }

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_account_email_bind;
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
    }

    @Override
    public void initView() {
    }

    @Override
    public void initOperator() {

    }

    @Override
    public void onDestroy() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        super.onDestroy();
    }


    @OnClick({R.id.tv_send_verification_code, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_send_verification_code:
                String emailAddress = etEmailAddress.getText().toString();
                if (TextUtils.isEmpty(emailAddress) || !ValidatonCheckUtil.isEmail(emailAddress)) {
                    ToastUtil.getInstance().shortLength(context, "请输入有效的邮箱地址");
                    return;
                }
                // 发送邮箱验证码
                JSONObject paramWithSendVerifyCode = new JSONObject();
                try {
                    paramWithSendVerifyCode.put("emailaddress", emailAddress);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                presenter.userSendEmailVerifyCode(paramWithSendVerifyCode.toString());
                timer.start();

                break;
            case R.id.tv_confirm:
                String email = etEmailAddress.getText().toString();
                String verifyCode = etAuthenticationCode.getText().toString();
                if (TextUtils.isEmpty(email) || TextUtils.isEmpty(verifyCode)) {
                    ToastUtil.getInstance().shortLength(context, "请输入邮箱地址和验证码");
                    return;
                }
                JSONObject parmWithBindEmail = new JSONObject();
                try {
                    parmWithBindEmail.put("email", email);
                    parmWithBindEmail.put("code", verifyCode);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                presenter.userBindEmail(parmWithBindEmail.toString());
                break;
        }
    }

    @Override
    public void onUserSendEmailVerifyCodeSuccess(String code) {
        super.onMsgGetVerifyCodeSuccess(code);
        ToastUtil.getInstance().shortLength(context, "邮件发送成功");
    }

    @Override
    public void onUserSendEmailVerifyCodeFail(Exception e) {
        super.onMsgGetVerifyCodeFail(e);
        ToastUtil.getInstance().shortLength(context, "" + e.getMessage());
    }

    @Override
    public void onUserBindEmailSuccess(String resp) {
        super.onUserBindEmailSuccess(resp);
        ToastUtil.getInstance().shortLength(context, "绑定成功");
        String email = etEmailAddress.getText().toString();
        entityUser.setEmail(resp);
        SharedpreferenceUtil.getInstance().saveUserInfo(context,entityUser);
        finish();
    }

    @Override
    public void onUserBindEmailFail(Exception e) {
        super.onUserBindEmailFail(e);
        ToastUtil.getInstance().shortLength(context, "" + e.getMessage());
    }
}
